| 1 | package net.mandaria.tippytipper.preferences; |
| 2 | |
| 3 | /* The following code was written by Matthew Wiggins |
| 4 | * and is released under the APACHE 2.0 license |
| 5 | * |
| 6 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | */ |
| 8 | |
| 9 | import net.mandaria.tippytipper.*; |
| 10 | import android.annotation.SuppressLint; |
| 11 | import android.content.Context; |
| 12 | import android.util.AttributeSet; |
| 13 | import android.view.Gravity; |
| 14 | import android.view.View; |
| 15 | import android.view.ViewGroup.LayoutParams; |
| 16 | import android.preference.DialogPreference; |
| 17 | import android.widget.SeekBar; |
| 18 | import android.widget.TextView; |
| 19 | import android.widget.LinearLayout; |
| 20 | import android.content.res.*; |
| 21 | |
| 22 | public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener { |
| 23 | private static final String androidns = "http://schemas.android.com/apk/res/android"; |
| 24 | |
| 25 | private SeekBar mSeekBar; |
| 26 | private TextView mSplashText, mValueText; |
| 27 | private Context mContext; |
| 28 | |
| 29 | private String mDialogMessage, mSuffix; |
| 30 | private int mDefault, mMax, mMin, mValue = 0; |
| 31 | |
| 32 | public SeekBarPreference(Context context, AttributeSet attrs) { |
| 33 | super(context, attrs); |
| 34 | mContext = context; |
| 35 | |
| 36 | mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage"); |
| 37 | mSuffix = attrs.getAttributeValue(androidns, "text"); |
| 38 | mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0); |
| 39 | mMax = attrs.getAttributeIntValue(androidns, "max", 100); |
| 40 | |
| 41 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SeekBarPreference); |
| 42 | mMin = a.getInt(R.styleable.SeekBarPreference_min, 0); |
| 43 | mMax = mMax - mMin; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | protected View onCreateDialogView() { |
| 48 | LinearLayout.LayoutParams params; |
| 49 | LinearLayout layout = new LinearLayout(mContext); |
| 50 | layout.setOrientation(LinearLayout.VERTICAL); |
| 51 | layout.setPadding(6, 6, 6, 6); |
| 52 | |
| 53 | mSplashText = new TextView(mContext); |
| 54 | if (mDialogMessage != null) |
| 55 | mSplashText.setText(mDialogMessage); |
| 56 | layout.addView(mSplashText); |
| 57 | |
| 58 | mValueText = new TextView(mContext); |
| 59 | mValueText.setGravity(Gravity.CENTER_HORIZONTAL); |
| 60 | mValueText.setTextSize(32); |
| 61 | params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); |
| 62 | layout.addView(mValueText, params); |
| 63 | |
| 64 | mSeekBar = new SeekBar(mContext); |
| 65 | mSeekBar.setOnSeekBarChangeListener(this); |
| 66 | layout.addView(mSeekBar, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); |
| 67 | |
| 68 | if (shouldPersist()) |
| 69 | mValue = getPersistedInt(mDefault); |
| 70 | |
| 71 | mSeekBar.setMax(mMax); |
| 72 | mSeekBar.setProgress(mValue); |
| 73 | return layout; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | protected void onBindDialogView(View v) { |
| 78 | super.onBindDialogView(v); |
| 79 | mSeekBar.setMax(mMax); |
| 80 | mSeekBar.setProgress(mValue - mMin); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | protected void onSetInitialValue(boolean restore, Object defaultValue) { |
| 85 | super.onSetInitialValue(restore, defaultValue); |
| 86 | if (restore) { |
| 87 | try { |
| 88 | mValue = shouldPersist() ? getPersistedInt(mDefault) : 0; |
| 89 | } catch (Exception ex) { |
| 90 | mValue = mDefault; |
| 91 | } |
| 92 | } else |
| 93 | mValue = (Integer) defaultValue; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) { |
| 98 | String t = String.valueOf(value + mMin); |
| 99 | mValueText.setText(mSuffix == null ? t : t.concat(mSuffix)); |
| 100 | if (shouldPersist()) |
| 101 | persistInt(value + mMin); |
| 102 | callChangeListener(new Integer(value + mMin)); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onStartTrackingTouch(SeekBar seekBar) { |
| 107 | // TODO Auto-generated method stub |
| 108 | |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void onStopTrackingTouch(SeekBar seekBar) { |
| 113 | // TODO Auto-generated method stub |
| 114 | |
| 115 | } |
| 116 | } |